package cn.it.Service.impl;

import cn.it.Service.IGoodsService;
import cn.it.domain.Business;
import cn.it.domain.Goods;
import cn.it.mapper.BusinessMapper;
import cn.it.mapper.GoodsMapper;
import cn.it.util.MyException;
import cn.it.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    BusinessMapper businessMapper;

    @Override
    public List<Goods> queryGoodsByType(Integer typeid) throws MyException {
        if (typeid==null){
            throw new MyException("请传入正确的id");

        }else{
            if (typeid==0){
               return goodsMapper.queryGoodsAll();
            }else{
                return goodsMapper.queryGoodsByType(typeid);
            }

        }

    }

    @Override
    public Result queryGoodsByID(Integer id) {
        if (id==null){
           return new Result(201,"不存在id" ,null);
        }else{
            Goods goods = goodsMapper.queryGoodsById(id);
            if (goods==null){
                return new Result(202,"商品不存在" ,null);
            }else{
                Integer businessId = goods.getBusinessId();
                Business business = businessMapper.queryById(businessId);
                HashMap<String,Object> map = new HashMap<>();
                map.put("goods",goods);
                map.put("business",business);
                return new Result(200,"查询成功",map);
            }


        }
    }
}
